//
//  Banner.m
//  Dianping
//
//  Created by 祝文博 on 15/3/10.
//  Copyright (c) 2015年 KevinLab. All rights reserved.
//

#import "Banner.h"
#import "UIImageView+WebCache.h"
#import "NSObject+Additions.h"

@implementation Banner

- (instancetype)initWithAttributes:(NSDictionary *)attributes
{
    if (self = [super init]) {
        self.title = [attributes[@"title" ] toString];
        self.subTitle = [attributes[@"subTitle"] toString];
        self.imageURL = [attributes[@"imageURL"] toString];
        self.targetURL = [[attributes[@"targetURL"] toString] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
        
        SDImageCache *imageCache = [SDImageCache sharedImageCache];
        UIImage *image = [imageCache imageFromDiskCacheForKey:self.imageURL];
        if (image == nil) {
            image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:self.imageURL]]];
            if (image) {
                [imageCache storeImage:image forKey:self.imageURL];
            }
        }
        
        if (image) {
            //1080 / 640 = 1.6875
            //1080 / 750 = 1.44
            CGFloat scale = image.size.width / BANNER_IMAGE_WIDTH;
//            NSLog(@"-----%f,%f", image.size.width,image.size.height);
            image = [image scaleToSize:CGSizeMake(BANNER_IMAGE_WIDTH, image.size.height / scale)];
//            NSLog(@"+++++%f,%f,%f", image.size.width,image.size.height);
        }
        self.image = image;
    }
    return self;
}

@end
